package com.bwie.commodity.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.bwie.commodity.domain.dto.CommodityCategoryDTO;
import com.bwie.commodity.domain.vo.CommodityCategoryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bwie.commodity.mapper.CommodityCategoryMapper;
import com.bwie.commodity.domain.CommodityCategory;
import com.bwie.commodity.service.ICommodityCategoryService;
import org.springframework.util.CollectionUtils;

/**
 * 商品分类Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-14
 */
@Service
public class CommodityCategoryServiceImpl implements ICommodityCategoryService
{
    @Autowired
    private CommodityCategoryMapper commodityCategoryMapper;

    /**
     * 查询商品分类
     *
     * @param id 商品分类主键
     * @return 商品分类
     */
    @Override
    public CommodityCategory selectCommodityCategoryById(Long id)
    {
        return commodityCategoryMapper.selectCommodityCategoryById(id);
    }

    /**
     * 查询商品分类列表
     *
     * @param commodityCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<CommodityCategory> selectCommodityCategoryList(CommodityCategory commodityCategory)
    {
        return commodityCategoryMapper.selectCommodityCategoryList(commodityCategory);
    }

    /**
     * 新增商品分类
     *
     * @param commodityCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertCommodityCategory(CommodityCategory commodityCategory)
    {
        commodityCategoryMapper.insertCommodityCategory(commodityCategory);

        //添加分类属性中间表
        return commodityCategoryMapper.insertCommodityCategoryAttributes(commodityCategory.getId(),commodityCategory.getCommodityTypeAttributesId());
    }

    /**
     * 修改商品分类
     *
     * @param commodityCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateCommodityCategory(CommodityCategory commodityCategory)
    {
        commodityCategoryMapper.updateCommodityCategory(commodityCategory);

        //根据商品分类ID删除分类属性中间表
        commodityCategoryMapper.delCommodityCategoryAttributesByCategoryId(commodityCategory.getId());

        //添加分类属性中间表
        return commodityCategoryMapper.insertCommodityCategoryAttributes(commodityCategory.getId(), commodityCategory.getCommodityTypeAttributesId());
    }

    /**
     * 批量删除商品分类
     *
     * @param ids 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCommodityCategoryByIds(Long[] ids)
    {
        for (Long id : ids) {
            //根据pId查询商品分类
            CommodityCategory category = commodityCategoryMapper.selectCommodityCategoryByPid(id);

            //根据pId删除商品分类
            if (category!=null){
                commodityCategoryMapper.deleteCommodityCategoryByPid(id);
            }
        }

        //根据id删除商品分类
        return commodityCategoryMapper.deleteCommodityCategoryByIds(ids);
    }

    /**
     * 删除商品分类信息
     *
     * @param id 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteCommodityCategoryById(Long id)
    {
        return commodityCategoryMapper.deleteCommodityCategoryById(id);
    }

    /**
     * 商品分类级联下拉框
     * @return
     */
    @Override
    public List<CommodityCategoryDTO> getCategoryTree() {
        List<CommodityCategoryDTO> oneTree = commodityCategoryMapper.getCategoryTree(0);

        //获取子集
        getTwoTree(oneTree);

        return oneTree;
    }

    /**
     * 获取子集
     * @param oneTree
     */
    private void getTwoTree(List<CommodityCategoryDTO> oneTree) {
        oneTree.forEach(commodityCategoryDTO -> {
            List<CommodityCategoryDTO> twoTree = commodityCategoryMapper.getCategoryTree(commodityCategoryDTO.getId());

            if (!CollectionUtils.isEmpty(twoTree)){
                getTwoTree(twoTree);
            }

            commodityCategoryDTO.setChildren(twoTree);
        });
    }
}
